package com.patrol.controller;

import com.patrol.pojo.RiskPoint;
import com.patrol.pojo.TaskPoint;
import com.patrol.service.RiskPointService;
import com.patrol.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zsc
 * @description: 风险点位管理接口
 */
@CrossOrigin
@Api(tags = "风险点位管理接口")
@RestController
@RequestMapping("/api/riskpoint")
public class RiskPointController {

    @Autowired
    private RiskPointService riskPointService;

    /**
     * 添加点位
     * @param riskPoint
     * @return
     */
    @ApiOperation(value = "添加点位")
    @PostMapping("insertPoint")
    public Result insertPoint(@RequestBody RiskPoint riskPoint){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        riskPoint.setCreateTime(df.format(new Date()));
        riskPoint.setUpdateTime(df.format(new Date()));
        boolean flag = riskPointService.save(riskPoint);
        System.out.println("Hello");
        if (flag) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 根据ID查询点位
     * @param Id
     * @return
     */
    @ApiOperation(value = "根据ID查询点位")
    @GetMapping("selectPointById/{Id}")
    public Result selectPointById(@PathVariable Integer Id){
        RiskPoint riskPoint = riskPointService.selectById(Id);
        return Result.ok(riskPoint);
    }

    /**
     * 查询所有风险点位
     * @param
     * @return
     */
    @ApiOperation(value = "查询所有风险点位")
    @GetMapping("selectAll")
    public Result selectAll(){
        List<RiskPoint> list = riskPointService.selectAllRiskPoint();
        return Result.ok(list);
    }

    /**
     * 根据ID逻辑删除风险点位
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID逻辑删除风险点位")
    @DeleteMapping("{id}")
    public Result deleteOneById(@PathVariable Long id){
        boolean flag = riskPointService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 批量逻辑删除风险点位
     * @param idList
     * @return
     */
    @ApiOperation(value = "批量逻辑删除点位")
    @DeleteMapping("batchRemove")
    public Result batchRemovePoint(@RequestBody List<Long> idList){
        riskPointService.removeByIds(idList);
        return Result.ok();
    }
}
